package com.caojing.haolemai.ui.category;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.caojing.haolemai.R;
import com.caojing.haolemai.base.BaseFragment;
import com.caojing.haolemai.bean.categorybean.BrandCategoryBean;
import com.caojing.haolemai.bean.categorybean.CategoryBean;
import com.caojing.haolemai.bean.categorybean.CategoryResultsBean;
import com.caojing.haolemai.ui.category.categoryadapter.CategoryLeftAdapter;
import com.caojing.haolemai.ui.category.categoryrightfragment.CategoryRightFragment;
import com.caojing.haolemai.ui.search.SearchActivity;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * A simple {@link Fragment} subclass.
 */
public class CategoryFragment extends BaseFragment implements CatrgoryContract.View{


    @Bind(R.id.rl_category_search)
    RelativeLayout rlCategorySearch;
    @Bind(R.id.lv_category_left)
    ListView lvCategoryLeft;
    @Bind(R.id.fl_category_right_container)
    FrameLayout flCategoryRightContainer;
    private CategoryLeftAdapter adapter;

    private CatrgoryContract.Presenter presenter;

    public CategoryFragment() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_category;
    }
    @Override
    protected void initView(View view) {
        presenter=new CategoryPresenter(this);


    }

    @Override
    protected void initData() {
        presenter.initCategoryData();
        lvCategoryLeft.setAdapter(adapter);
    }

   

    @OnClick(R.id.rl_category_search)
    public void onClick() {
        Intent intent = new Intent(getActivity(), SearchActivity.class);
        startActivity(intent);
    }

    @Override
    public void onSuccessGetCategoryBean(CategoryBean categoryBean) {
        final List<CategoryResultsBean> results = categoryBean.getResults();
//        Log.i("TAG","results---------------------"+results.size());
        adapter = new CategoryLeftAdapter(getActivity(),results);
        lvCategoryLeft.setAdapter(adapter);

        //默认第一个被选中
        CategoryResultsBean brandList = results.get(0);
        List<BrandCategoryBean> brandCategory = brandList.getBrandCategory();
        List<CategoryResultsBean.SecondBean> secondBeen = brandList.getSecond();
        FragmentManager childFragmentManager = getChildFragmentManager();
        FragmentTransaction transaction = childFragmentManager.beginTransaction();
        CategoryRightFragment rightFragment=new CategoryRightFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("brandCategory", (Serializable) brandCategory);
        bundle.putSerializable("secondBeen", (Serializable) secondBeen);
        rightFragment.setArguments(bundle);
        transaction.add(R.id.fl_category_right_container,rightFragment);
        //不把commit修改成下面的代码，回报错java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
        transaction.commitAllowingStateLoss();

        //点击显示右边的详情
        lvCategoryLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CategoryResultsBean resultsBean = results.get(position);
                List<BrandCategoryBean> brandCategory = resultsBean.getBrandCategory();
                List<CategoryResultsBean.SecondBean> secondBeen = resultsBean.getSecond();

                FragmentManager childFragmentManager = getChildFragmentManager();
                FragmentTransaction transaction = childFragmentManager.beginTransaction();
                CategoryRightFragment rightFragment=new CategoryRightFragment();
                Bundle bundle = new Bundle();
                bundle.putSerializable("brandCategory", (Serializable) brandCategory);
                bundle.putSerializable("secondBeen", (Serializable) secondBeen);
                rightFragment.setArguments(bundle);
                transaction.replace(R.id.fl_category_right_container,rightFragment);
                transaction.commitAllowingStateLoss();
            }
        });

    }

    @Override
    public void onFailGetCategoryBean(String msg) {
        //Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
    }
}
